/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */


package com.android1.amarena2d.nodes.layer;

import com.android1.amarena2d.engine.AmarenaCamera;
import com.android1.amarena2d.engine.GL;
import com.android1.amarena2d.nodes.LayerNode;
import com.android1.amarena2d.nodes.Scene;

//Layer with its own Camera
public class CamLayer extends LayerNode {

    protected AmarenaCamera camera = new AmarenaCamera();
    protected int camWidth = 0;
    protected int camHeight = 0;
    protected int surfaceWidth;
    protected int surfaceHeight;


    @Override
    public void init() {

        surfaceWidth = engine.getGraphics().getWidth();
        surfaceHeight = engine.getGraphics().getHeight();

        camWidth = engine.getConfig().getCameraWith();
        if (camWidth == 0)
            camWidth = engine.getGraphics().getWidth();
        camHeight = engine.getConfig().getCameraHeight();
        if (camHeight == 0)
            camHeight = engine.getGraphics().getHeight();

        camera.setViewport(camWidth, camHeight);
        //set camera so that bottom-left is 0,0
        camera.setXY(camWidth / 2F, camHeight / 2F);

        super.init();
    }

    @Override
    public AmarenaCamera getCamera() {
        return camera;
    }

    @Override
    public void render() {
        Scene.CAM = camera;
        applyCam();
        super.render();
        Scene.CAM = engine.getRenderer().getCamera();
        engine.getRenderer().applyCam();
    }

    protected void applyCam() {

//        GL.getGl10().glViewport(0, 0, surfaceWidth, surfaceHeight);
        camera.setMatrices();
        camera.updateVisibleRect();
        GL.getGl10().glLoadIdentity();


    }

}
